package com.dz.basic.web;

import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dz.basic.Constants;
import com.dz.basic.entity.Item;
import com.dz.basic.entity.Labels;
import com.dz.basic.service.ItemService;
import com.dz.basic.service.LabelsService;
import com.dz.basic.vo.ProductSearchVO;

/**
 * <p>
 * 作为标签库，关联业务有客户、订单、商品等。 前端控制器
 * </p>
 *
 * @author star
 * @since 2017-06-27
 */
@RestController
public class RelationLabelsControler {

	@Autowired
	private LabelsService labelsService;

	@Autowired
	private ItemService itemService;

	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "productSearch", method = RequestMethod.GET)
	@ApiOperation(value = "查询产品筛选内容", httpMethod = "GET")
	public List<ProductSearchVO> queryVO() {
		List<ProductSearchVO> productSearchVOs = new ArrayList<ProductSearchVO>();
		addLables(productSearchVOs);
		addItems(productSearchVOs, Constants.REPAYMENT_METHOD_TYPE, "还款方式");
		addItems(productSearchVOs, Constants.LOAN_TYPE, "贷款类型");
		return productSearchVOs;
	}

	@SuppressWarnings("rawtypes")
	private void addLables(List<ProductSearchVO> productSearchVOs) {
		ProductSearchVO<Item> productSearchVO = new ProductSearchVO<Item>();
		productSearchVO.setClassType("您的身份");
		Wrapper<Labels> laWrapper = new EntityWrapper<Labels>();
		laWrapper.eq("fitemClassid", Constants.PRODUCT_TYPE);
		List<Labels> laelList = labelsService.selectList(laWrapper);
		List<Item> itemList = new ArrayList<Item>();
		for (Labels labels : laelList) {
			Item item = new Item();
			item.setItemid(labels.getId());
			item.setItemdesc(labels.getName());
			item.setItemclassid(labels.getItemClassid().intValue());
			itemList.add(item);
		}
		productSearchVO.setChildList(itemList);
		productSearchVOs.add(productSearchVO);
	}

	@SuppressWarnings("rawtypes")
	public void addItems(List<ProductSearchVO> productSearchVOs, long type, String typeName) {
		ProductSearchVO<Item> productSearchVO = new ProductSearchVO<Item>();
		productSearchVO.setClassType(typeName);
		Wrapper<Item> laWrapper = new EntityWrapper<Item>();
		laWrapper.eq("fitemClassid", type);
		productSearchVO.setChildList(itemService.selectList(laWrapper));
		productSearchVOs.add(productSearchVO);
	}
}
